const httpServer = (options) => {
	//初始化配置
	let httpOptions = {
		url: options.url,
		data: options.data,
		method: options.method,
		header: {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json"
		}
	}
	const token = uni.getStorageSync('token')
	const gpToken = uni.getStorageSync('gpToken')
	if (token) {
		httpOptions.header['Authorization'] = 'Bearer ' + token
	}
	if (gpToken) {
		httpOptions.header['gpToken'] = gpToken
	}
	const noLoadingList = ['getRSAPubKey', 'workDataCount', 'getTenantsByUser', 'flowType', 'profile', 'flow_urgent_level']
	const containsKeyword = noLoadingList.some(keyword => httpOptions.url.includes(keyword));
	
	if (!containsKeyword) {
	  //添加遮罩
	  uni.showLoading({
	  	title:'请求中...'
	  })
	}
	let promise = new Promise((resolve, reject)=> {
		uni.request({
			...httpOptions,
			success: (res) => {
				uni.hideLoading()
				console.log('res------------', {...httpOptions});
				// 响应头中的值
				if (res.statusCode === 200) {
					console.log(res.data);
					if (res.data.Status == 0) {
						//请求成功返回参数
						resolve(res.data)
					} else  {
						let {Status = 0, msg = ''} = res.data
						if (Status == 100) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
							uni.showToast({
								title: msg,
								icon:'none'
							})
						} else {
							uni.showToast({
								title: msg,
								icon:'none'
							})
						}
					}
				} else if (res.statusCode == 404) {
					uni.showToast({
						title: '接口不存在，请联系技术人员',
						icon:'none'
					})
					return;
				} else {
					uni.hideLoading()
					if(res.data.code==401){
						uni.showToast({
							title: '请登录后操作',
							icon:'none'
						})
						setTimeout(() => {
							try {
							    uni.clearStorageSync();
							} catch (e) {
							    // error
							}
						}, 2000)
					}else{
						console.log('res.data.msg', res.data);
						setTimeout(() => {
							if (res.data.msg) {
								uni.showToast({
									title: res.data.msg,
									icon:'none'
								})
							}
						}, 200)
					}
					
				}
			},
			fail:(err)=> {
				reject(err)
				uni.hideLoading()
				setTimeout(() => {
					uni.showToast({
						title: '请求失败，请检查网络！',
						icon:'none'
					})
				}, 200)
			},
		});
	})
	return promise
}
export default httpServer